import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


dfuserpoint = pd.read_excel('发票识别导出-筛选.xlsx')
# dfuserpoint=pd.read_csv('发票识别导出-筛选.csv')

outpd = pd.DataFrame(dfuserpoint,
                     columns=['大单号', '主店片区', '主店号', '主店名', '主店所在公司', '现金', '公司礼券', '商场储值',
                              '订金销账', '积分抵现', '商场礼券', '活动名称', '日期', '银行卡', '小票网址'])
outpd.insert(loc=15, column='result', value='')
outpd.insert(loc=16, column='url', value='')

import requests

url_heard1 = "http://localhost:10111"
url_heard = "http://192.168.12.46:8888"
url_heard2 = "http://192.168.12.107:10111"

def check_receipt_get(url):
    # # 仅验证日期年份 即包含22，23
    check_url = url_heard1 + "/ReceiptORC/check_img"

    # re = ""
    # params = {'url':check_url}
    # headers = {'Connection': 'close'}
    # req = requests.get(url=check_url, headers=headers,params=params).content
    # re = req.text
    # req.close()

    #re = contents = urllib.request.urlopen(check_url).read()

    req = requests.post(check_url, data={"url": url})
    re = req.text
    return re


for index, row in outpd.iterrows():
    if not isinstance(row['小票网址'], str):
        outpd.iloc[index, outpd.columns.get_loc("result")] = ''
        continue

    result = check_receipt_get(row['小票网址'])

    outpd.iloc[index, outpd.columns.get_loc("result")] = result
    print(index, row['大单号'], result)

with pd.ExcelWriter('out-发票识别年份导出.xlsx') as writer:
    outpd.to_excel(writer, sheet_name='Sheet1')
